package com.purchase.ml.resource.api;

import com.purchase.ml.bean.api.Ipay88Back;
import com.purchase.ml.service.sdk.Ipay88Service;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

/**
 * @author :zxm
 * @date :2021/2/25 10:41
 * @description :some description
 **/
@RestController
@RequestMapping("/ipay88")
@Slf4j
@Api(tags = "经销商")
public class Ipay88CallbackControll {
    @Value("${dns.front_url}")
    private String frontUrl;

    @Autowired
    Ipay88Service ipay88Service;

    @PostMapping("/callback")
    public String ipay88CallBack(@Valid Ipay88Back dto) throws NoSuchAlgorithmException {
        log.info("callbackcallback==={}",dto);
        ipay88Service.reciveIpay88CallBack(dto);
        return "RECEIVEOK";
    }
//    reposenUrl==222=Ipay88Back(MerchantCode=M03228, PaymentId=2, RefNo=66167351, Amount=1.00, Currency=MYR, Remark=, TransId=T056779555221, AuthCode=, Status=0, ErrDesc=Permission not allow, Signature=, CCName=, CCNo=, S_bankname=, S_country=)
    @PostMapping("/reposenUrl")
    public void ipay88ReposenUrl(@Valid Ipay88Back dto,HttpServletResponse  response) throws Exception{
        log.info("reposenUrl==222={}",dto);
        String url = frontUrl + "/Shop/Query";
        url = url + "?" + "oid=" + dto.getRefNo() + "paymet="+dto.getAmount() + "state=" + dto.getStatus();
        ipay88Service.reciveIpay88CallBack(dto);
        response.sendRedirect(url);
    }
 //   2021-04-27 10:14:33.110  INFO 92465 --- [http-nio-50050-exec-6] c.p.m.r.api.Ipay88CallbackControll       :
//    reposenUrl==222=Ipay88Back(MerchantCode=M05185, PaymentId=6, RefNo=66868368, Amount=1.00, Currency=MYR, Remark=, TransId=T057425723721, AuthCode=2104271013400796, Status=1, ErrDesc=, Signature=934906e2a33bb066bf2adc77da1c3a2073522450f1af8210bd97988a54031f85, CCName=YAPBOONPING, CCNo=, S_bankname=MB2U0227, S_country=)

}
